/*
 * @Author: 陈德基
 * @Date: 2021-11-15 17:36:17
 * @LastEditTime: 2021-11-15 18:19:47
 * @Description: 
 * @FilePath: \copy_gsy_github_app_flutter\lib\db\sql_provider.dart
 * 联系方式：
 * wx: melody_2009
 * qq: 7815171323 
 */

import 'package:flutter/material.dart';
import 'package:sqflite/sqflite.dart';

import 'sql_manager.dart';

abstract class DBProvider {
  bool isTableExits = false;
  sqlCreateTable();
  tableName();
  tableBaseString(String name, String column) {
    return '''
      create table $name ($column integer primary key autoincrement,
     ''';
  }

  Future<Database> getDataBase() async {
    return await open();
  }

  @mustCallSuper
  prepare(name, String? createSql) async {
    isTableExits = await SqlDriver.isTableExits(name);
    if (!isTableExits) {
      Database? db = await SqlDriver.getCurrentDatabase();
      return await db?.execute(createSql!);
    }
  }

  @mustCallSuper
  open() async {
    if (!isTableExits) {
      await prepare(tableName(), sqlCreateTable());
    }
    return await SqlDriver.getCurrentDatabase();
  }
}
